home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
55375
/
55375.xpi
/
chrome
/
chromeFiles
/
content
/
sc_refinements.js
< prev
Wrap
Text File
|
2009-11-15
|
7KB
|
184 lines
(function () {
var btPrefBranch = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.brandthunder.");
try {
var scRefinements = btPrefBranch.getBoolPref("scRefinements");
if (!scRefinements) {
return;
}
} catch (ex) {
}
var partner = { uiLabel: 'Brand Thunder Refinements', partnerCode: 'bt', authCode: 'khb88305' };
function detectPageLoad(event) {
var boomClient = "";
try {
var currentBoom = btPrefBranch.getCharPref("currentBoom");
for (let i in BrandThunder.clients) {
for (let j in BrandThunder.clients[i].booms) {
if (j == currentBoom) {
boomClient = i;
break;
}
}
}
} catch(ex) {}
partner.subpartnerCode = boomClient;
scAddRefinementLinks(partner);
}
function firstRun() {
var appcontent = document.getElementById("appcontent");
if (appcontent) {
appcontent.addEventListener("DOMContentLoaded", detectPageLoad, false);
}
window.removeEventListener("load", firstRun, false);
}
function lastRun(event) {
window.removeEventListener("unload", lastRun, false);
}
window.addEventListener("load", firstRun, false);
window.addEventListener("unload", lastRun, false);
/* SURF CANYON BEGIN */
function scAddRefinementLinks(partner) {
// quit if SC or any other extension has already put the refinement links on the page
if (_content.document.getElementById('scTopOfPageRefinementLinks')) {
return;
}
try {
var host = _content.document.location.host;
var pathname = _content.document.location.pathname;
if (host) {
if (scContainsSubstring(host, '.google.') &&
scContainsSubstring(host,'www') &&
((pathname == '/') || (pathname == '/search'))) {
window.scRefinementQuery = null;
waitForGoogleAjaxToComplete(partner);
} else if (scContainsSubstring(host, 'search.yahoo.')) {
window.scRefinementQuery = _content.document.getElementById("yschsp").value;
var div = _content.document.getElementById('web');
var insertPoint = scGetDescendents(div, "ol")[0];
launchSCAjaxRequestForRefinementLinks(partner, insertPoint, '');
} else if (scContainsSubstring(host, '.bing.com') && ((pathname == '/search'))) {
window.scRefinementQuery = _content.document.getElementById("sb_form_q").value;
var div = _content.document.getElementById('results');
var insertPoint = scGetDescendents(div, "ul")[0];
launchSCAjaxRequestForRefinementLinks(partner, insertPoint, '');
}
}
} catch (ex) {
/* Not a web page */
}
}
function waitForGoogleAjaxToComplete(partner) {
if (!window.scRefinementQuery) {
var aTags = _content.document.getElementsByTagName('a');
for (var i = 0; (i < aTags.length) && !window.scRefinementQuery; i++) {
var aTag = aTags[i];
var href = aTag.href;
if (scContainsSubstring(href, '.google.') && (scContainsSubstring(href, '/advanced_search?q=') || scContainsSubstring(href,'/swr?q='))) {
window.scRefinementQuery = href.replace(/.*\?q=/, '').replace(/&.*/, '');
}
}
if (!window.scRefinementQuery) {
setTimeout(waitForGoogleAjaxToComplete, 200, partner);
return;
}
}
var liTags = _content.document.getElementsByTagName('li');
for (i = 0; i < liTags.length; i++) {
var liTag = liTags[i];
var cls = liTag.getAttribute('class');
if (cls && ((cls == 'g') || (cls.indexOf('g ') === 0))) {
var insertPoint = liTag.parentNode;
launchSCAjaxRequestForRefinementLinks(partner, insertPoint, '');
break;
}
}
}
function launchSCAjaxRequestForRefinementLinks(partner, insertPoint, yStyle) {
var div = _content.document.createElement("div");
div.innerHTML = '<div id=scTopOfPageRefinementLinks scTopPos=1 partner="' + partner.partnerCode + '" style="height: 20px; margin-top: 7px; margin-bottom: 7px;' + yStyle + '"></div>';
insertPoint.parentNode.insertBefore(div, insertPoint);
var url = 'http://' + partner.authCode + '.surfcanyon.com/queryReformulation?partner=' + partner.partnerCode + '&subpartner=' + partner.subpartnerCode + '&authCode=' + partner.authCode + '&q=' + window.scRefinementQuery.replace(/ /g, '+');
var xhr = new window.XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var parser = new window.DOMParser();
var text = xhr.responseText;
var xmlRoot = parser.parseFromString(text, "text/xml");
var itemNodes = xmlRoot.getElementsByTagName('refinement');
var items = [];
var runningLength = 0;
for (var i = 0; i < itemNodes.length; i++) {
try {
var itemNode = itemNodes[i];
var refinement = itemNode.textContent.toLowerCase();
var refinementLength = refinement.length;
if (runningLength + refinementLength < 80) {
runningLength += refinementLength;
var query = scMakeRefinementQuery(refinement).replace(/ /g, '+');
items.push('<a href="http://search.surfcanyon.com/search?f=nrl' + i + '&q=' + query + '&partner=' + partner.partnerCode + '&subpartner=' + partner.subpartnerCode + '">' + refinement + '</a>');
}
} catch (e) {
}
}
if (items.length > 0) {
var div = _content.document.getElementById('scTopOfPageRefinementLinks');
if (div && (div.getAttribute('partner') == partner.partnerCode)) {
div.innerHTML = '<font size=-1><b>' + items.join(' ') + '</b> <font size =-1 color=darkgray>' + partner.uiLabel + '</font></font>';
}
}
}
};
xhr.send(null);
}
function scMakeRefinementQuery(refinement) {
var query = refinement;
var words = window.scRefinementQuery.replace(/"'\(\),/g, '').replace(/\+/g, ' ').split(' ');
for (var i = 0; i < words.length; i++) {
var word = words[i];
if (!scContainsSubstring(query.toLowerCase(), word.toLowerCase())) {
query = query + ' ' + word;
}
}
return query;
}
function scGetDescendents(node, tagName) {
var arr = [];
if (node && node.childNodes) {
var childNodes = node.childNodes;
for (var i = 0; i < childNodes.length; i++) {
var child = childNodes.item(i);
if (child.tagName && (!tagName || (child.tagName.toUpperCase() == tagName.toUpperCase()))) {
arr[arr.length] = child;
}
if (child.hasChildNodes()) {
arr = arr.concat(scGetDescendents(child, tagName));
}
}
}
return arr;
}
function scContainsSubstring(text, substring) {
return text && substring && (text.indexOf(substring) >= 0);
}
/* SURF CANYON END */
})();